package com.tstudy.laoshibang.share;

import java.util.ArrayList;

import com.tencent.connect.UserInfo;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

/**
 * @author wuyahui
 */
public class WrapQQ{

	public static final int QZONE_TYPE_BIND = 0;
	public static final int QZONE_TYPE_SHARE = 1;
	public static final int QZONE_TYPE_GET_FRIENDS = 2;
	private static WrapQQ instance = new WrapQQ();
	public Tencent mTencent;
	
	public WrapQQ(){
		init();
	}
	
	
	public static WrapQQ getInstance(){
		return instance;
	}
	public void init(){
		mTencent = Tencent.createInstance(CONSTANT.QQ_APP_ID, BaseApplication.mContext);
	}
	
	public boolean isLogin(){
		boolean isLogin = false;
		if (mTencent != null && mTencent.isSessionValid()) {
			isLogin = true;
		}
		return isLogin;
	}
	
	public void login(Fragment fragment,IUiListener loginListener){
		init();
		mTencent.login(fragment, "all", loginListener);
//		if (!mTencent.isSessionValid()) {
//		}
	}
	
	/**
	 * {"is_yellow_year_vip":"0","ret":0,"figureurl_qq_1":"http:\/\/q.qlogo.cn\/qqapp\/1104517367\/88D686E97A6C66643842442AF257E325\/40","figureurl_qq_2":"http:\/\/q.qlogo.cn\/qqapp\/1104517367\/88D686E97A6C66643842442AF257E325\/100","nickname":"信仰","yellow_vip_level":"0","is_lost":0,"msg":"","city":"海淀","figureurl_1":"http:\/\/qzapp.qlogo.cn\/qzapp\/1104517367\/88D686E97A6C66643842442AF257E325\/50","vip":"0","level":"0","figureurl_2":"http:\/\/qzapp.qlogo.cn\/qzapp\/1104517367\/88D686E97A6C66643842442AF257E325\/100","province":"北京","is_yellow_vip":"0","gender":"男","figureurl":"http:\/\/qzapp.qlogo.cn\/qzapp\/1104517367\/88D686E97A6C66643842442AF257E325\/30"}
	{is_yellow_year_vip=0, ret=0, figureurl_qq_1=http://q.qlogo.cn/qqapp/1104517367/88D686E97A6C66643842442AF257E325/40, figureurl_qq_2=http://q.qlogo.cn/qqapp/1104517367/88D686E97A6C66643842442AF257E325/100, nickname=信仰, yellow_vip_level=0, is_lost=0, msg=, city=海淀, figureurl_1=http://qzapp.qlogo.cn/qzapp/1104517367/88D686E97A6C66643842442AF257E325/50, vip=0, level=0, figureurl_2=http://qzapp.qlogo.cn/qzapp/1104517367/88D686E97A6C66643842442AF257E325/100, province=北京, is_yellow_vip=0, gender=男, figureurl=http://qzapp.qlogo.cn/qzapp/1104517367/88D686E97A6C66643842442AF257E325/30}
	 * @param uiListener
	 * @return
	 */
	public UserInfo getUserInfo(BaseUIListener uiListener){
		UserInfo info = null;
		if (isLogin() && !TextUtils.isEmpty(mTencent.getQQToken().getOpenId())) {
			info = new UserInfo(BaseApplication.mContext,mTencent.getQQToken());
			info.getUserInfo(uiListener);
		}
		return info;
	}
	
	IUiListener qqShareListener;
	/**
	 * 
	 * @param title  分享的标题, 最长30个字符。
	 * @param summary 分享的消息摘要，最长40个字符。
	 * @param targetUrl
	 * @param imageUrl
	 * @param listener
	 */
	public void shareToQQ(String title,String summary,String targetUrl,String imageUrl,IUiListener listener) { 
		qqShareListener = listener;
		final Bundle params = new Bundle();
	    init();
	    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
	    params.putString(QQShare.SHARE_TO_QQ_TITLE, CommonUtil.subStr(title, 30));
	    params.putString(QQShare.SHARE_TO_QQ_SUMMARY,CommonUtil.subStr(summary, 40));
	    params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,targetUrl);
	    if (TextUtils.isEmpty(imageUrl)) {
	    	imageUrl = CommonUtil.getLogoImagePath();
			if (TextUtils.isEmpty(imageUrl)) {
				imageUrl = "file://" + CommonUtil.saveLogoImagePath();
			}
		}
		params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,imageUrl);
	    params.putString(QQShare.SHARE_TO_QQ_APP_NAME, BaseApplication.getResString(R.string.app_name));
	    mTencent.shareToQQ(BaseApplication.mCurrentActivity, params, listener);
	}

	public void shareToQzone (String title,String summary,String targetUrl,ArrayList<String> imageUrl,IUiListener listener) {
		qqShareListener = listener;
		init();
		final Bundle params = new Bundle();
		params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT );
		params.putString(QzoneShare.SHARE_TO_QQ_TITLE, CommonUtil.subStr(title, 30));//必填
		params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY,CommonUtil.subStr(summary, 40));//选填
		params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, targetUrl);//必填
	    params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrl);
	    mTencent.shareToQzone(BaseApplication.mCurrentActivity, params, listener);
		}
	
	public void activityResult(Intent data) {
		Tencent.handleResultData(data, qqShareListener);
	}
}
